package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/5/18
 */
@Service
public  class DtsGoodsProductServiceImpl implements DtsGoodsProductService {
    @Autowired
    DtsGoodsProductMapper productMapper;
    @Override
    public Integer getTotal() {
        return productMapper.selectCount(null);
    }

    @Override
    public void uddateProducts(Integer id, DtsGoodsProduct[] products) {
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId,id );
        productMapper.delete(qw);

        if(products!=null&&products.length>0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(id);
                productMapper.insert(product);
            }
        }
    }

    @Override
    public void insert(Integer id, DtsGoodsProduct[] products) {
        if(products!=null&&products.length>0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(id);
                productMapper.insert(product);
            }
        }
    }

    @Override
    public void delete(DtsGoods goods) {
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper();
        qw.eq(DtsGoodsProduct::getGoodsId, goods.getId());
        productMapper.delete(qw);
    }

    @Override
    public void deleteByGoodsId(DtsGoods goods) {
        DtsGoodsProduct attribute = new DtsGoodsProduct();
        attribute.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId, goods.getId());
        productMapper.update(attribute, qw);
    }
}
